package com.example.demo.controller;

import com.example.demo.dao.CityMapper;
import com.example.demo.dao.DzdpSkuMapper;
import com.example.demo.domain.City;
import com.example.demo.domain.CityExample;
import com.example.demo.domain.DzdpSkuExample;
import com.example.demo.dto.RankDto;
import com.example.demo.service.CityService;
import com.example.demo.service.RankService;
import com.example.demo.service.SkuService;

import com.example.demo.service.impl.DZDPScratchImpl;
import org.jsoup.Jsoup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author ZhengWei
 * @Description
 * @date 2020/7/16 11:05 上午
 */

@RestController
@RequestMapping("/scratch")
public class ScratchController {
    @Autowired
    private CityService cityService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private RankService rankService;
    @Autowired
    private DzdpSkuMapper skuMapper;
    @Autowired
    private CityMapper cityMapper;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @GetMapping("/sku")
    public void getSku(@RequestParam(value = "cityId", defaultValue = "1") Integer cityId) {
        skuService.getSkuList(cityId);
    }

    @GetMapping("/city")
    public void getCity(@RequestParam(value = "skuId", defaultValue = "10017670") Integer skuId) {
        cityService.getCityList(skuId);
    }

    @GetMapping("/rank")
    public List<RankDto> getRank(@RequestParam(value = "cityId") Integer cityId, @RequestParam(value = "skuId") Integer skuId) {
        List<RankDto> rankList = rankService.getRankList(cityId, skuId);
        System.out.println(Thread.currentThread());
        return rankList;
    }

    @GetMapping("/allSku")
    public void getAllSku(@RequestParam(value = "cityId",defaultValue = "0")Integer cityId) {
        long start = System.currentTimeMillis();

        CityExample cityExample = new CityExample();
        cityExample.createCriteria().andIdIsNotNull();
        cityExample.setOrderByClause("cityid ASC");
        List<City> allCity = cityMapper.selectByExample(cityExample);

        List<Integer> cityIdList = allCity.stream()
                .filter(o->o.getCityid()>88)
                .map(o -> o.getCityid())
                .collect(Collectors.toList());
        if (cityId != 0) {
            cityIdList.clear();
            cityIdList.add(cityId);
        }
        for (Integer cid : cityIdList) {
            getSku(cid);
        }
        long end = System.currentTimeMillis();
        long s = (end - start) / 1000;
        long m = s / 60;
        long h = m / 60;
        logger.info("总时长：" + h + " 小时 " + m % 60 + " 分钟 " + s % 60 + " 秒");
    }
}
